/* 
 * Copyright (c) 20160510, S.F. Express Inc. All rights reserved.
 */
package com.sf.apache.commons.samples;

import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.time.StopWatch;

/**
 * 描述：日期处理工具类
 * <p>
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    20150510       Robert          Create
 * ****************************************************************************
 * </pre>
 *
 * @author Robert
 * @since 1.0
 */
public class DateUtilsDemo {

	public final static String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss SSS";
	public final static String ORA_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 日期格式化
	 *
	 * @param pattern
	 */
	private static void print(String pattern) {

		// 工具类DateFormatUtils
		System.out.println(DateFormatUtils.format(new Date(), pattern));
	}

	/**
	 * 日期比较
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int compareTo(Date date1, Date date2) {

		// 使用默认时区和语言环境获得一个日历
		Calendar oldCal = Calendar.getInstance();
		Calendar newCal = Calendar.getInstance();

		// 使用给定的 Date 设置此 Calendar 的时间
		oldCal.setTime(date1);
		newCal.setTime(date2);
		return oldCal.compareTo(newCal);
	}

	public static void main(String[] args) {

		print(DATE_TIME_FORMAT);
		print(ORA_DATE_TIME_FORMAT);
		print(DateFormatUtils.ISO_DATETIME_FORMAT.getPattern());

		// 使用DateUtils增加时间
		System.out.println(compareTo(new Date(), DateUtils.addMinutes(new Date(), 1)));

		System.out.println("格式化日期输出.");
		System.out.println(DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));

		System.out.println("秒表.");
		StopWatch sw = new StopWatch();
		sw.start();

		for (Iterator<Calendar> iterator = DateUtils.iterator(new Date(), DateUtils.RANGE_WEEK_CENTER); iterator
				.hasNext();) {
			Calendar cal = iterator.next();
			System.out.println(DateFormatUtils.format(cal.getTime(), "yy-MM-dd HH:mm"));
		}

		sw.stop();
		System.out.println("秒表计时:" + sw.getTime());

	}
}
